import Vue from 'vue'
import Vuex from 'vuex'
import Request from '../common/request'
import UserApi from '@/api/user';

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    // 是否强制登陆
    forcedLogin: false,
    // 是否登陆
    hasLogin: false,
    // 用户名&手机号
    user: {},
    univerifyErrorMsg: "",
    hideUniverify: true,
    token: uni.getStorageSync('token')
  },
  mutations: {
    login(state, user) {
      state.user = user;
      state.hasLogin = true;
      state.token = user.token
      uni.setStorageSync('token', state.token)
    },
    logout(state) {
      state.user = {};
      state.hasLogin = false;
      state.token = ""
      uni.removeStorageSync('token')
    },
    setUser(state, user) {
      state.user = user
      state.hasLogin = !!user
    },
    setUniverifyErrorMsg(state, payload = '') {
      state.univerifyErrorMsg = payload
    },
    setHideUniverify(state, payload = false) {
      state.hideUniverify = payload
    }
  },
  actions: {
    async getUserInfo({ commit }, config) {
      const user = await Request.post(UserApi.getUserInfo, {}, config)
      commit('setUser', user)
      return user
    }
  }
})

export default store